<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>新闻分类列表</title>
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/plugins/jsTree/style.min.css" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/animate.min.css" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/style.min.css?v=4.0.0" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/views/sysdefault/css/plugins/toastr/toastr.min.css" rel="stylesheet">
	
</head>

<body class="gray-bg">
    
    <div class="animated fadeInRight">
    	<div class="row">
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>新闻分类列表</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                    
                    	<div class="m-b-sm">
                    		<button type="button" class="btn btn-success btn-sm" onclick="add();"><i class="fa fa-plus-square"></i>&nbsp;&nbsp;添加</button>
                    		<!-- <button type="button" class="btn btn-primary btn-sm" onclick="editFunc();"><i class="fa fa-edit"></i>&nbsp;&nbsp;编辑</button> -->
                    		<button type="button" class="btn btn-danger btn-sm" onclick="del();"><i class="fa fa-times-circle"></i>&nbsp;&nbsp;删除</button>
                    	</div>
                    	
                    	<div id="jstree"></div>
                        
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5 id="funcTitle">添加/编辑新闻分类</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        
                        <form id="subForm" method="post" class="form-horizontal" onsubmit="return subForm();" >
                        	<input type="hidden" id="mId" name="cateId" />
                        	<div class="form-group">
                        		<label class="col-sm-2 control-label">分类名称：</label>
                            	<div class="col-sm-10">
                            		<input type="text" id="mName" name="name" placeholder="请输入分类名称" class="form-control" required="" maxlength="15" >
                            	</div>
                        	</div>
                        	<div class="form-group">
                            	<label class="col-sm-2 control-label">排序：</label>
                            	<div class="col-sm-4">
                            		<input type="number" id="mSort" name="sort" class="form-control" >
                            	</div>
                        	</div>
                        	<div class="form-group">
								<label class="col-sm-2 control-label">分类号：</label>
                            	<div class="col-sm-10">
                            		<input type="text" id="mCode" class="form-control" readonly="readonly" >
                            	</div>
							</div>
                        	<div class="form-group">
                        		<label class="col-sm-2 control-label">上级分类：</label>
                            	<div class="col-sm-10">
                            		<input type="hidden" name="pid" value="0" >
                            		<input type="text" name="pName" disabled="" placeholder="请选择上级分类" class="form-control">
                            	</div>
                        	</div>
                        	
                        	<div class="hr-line-dashed"></div>
                        	<div class="form-group">
                                <div class="col-sm-6 col-sm-offset-2">
                                    <button class="btn btn-primary" type="submit" >保存内容</button>
                                    <button class="btn btn-white" onclick="clearForm();" >清空</button>
                                    
                                </div>
                            </div>
                        
                        </form>
                        
                    </div>
                </div>
            </div>
        </div>
    
    </div>
</body>   
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/jquery.min.js?v=2.1.4"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/bootstrap.min.js?v=3.3.5"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/content.min.js?v=1.0.0"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/plugins/jsTree/jstree.min.js"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/plugins/toastr/toastr.min.js"></script>
    <script src="<%=request.getContextPath()%>/views/sysdefault/js/plugins/layer/layer.js"></script>
	<script src="<%=request.getContextPath()%>/views/sysdefault/js/plugins/validate/messages_zh.min.js"></script>
    
    <script type="text/javascript">
    
    toastr.options = {
  		  "closeButton": true,
  		  "debug": false,
  		  "progressBar": true,
  		  "positionClass": "toast-top-left",
  		  "showDuration": "400",
  		  "hideDuration": "1000",
  		  "timeOut": "3000",
  		  "extendedTimeOut": "1000",
  		  "showEasing": "swing",
  		  "hideEasing": "linear",
  		  "showMethod": "fadeIn",
  		  "hideMethod": "fadeOut"
  		}
    
    var basePath = '<%=request.getContextPath()%>';
    
    //初始化页面
    $(function(){
    	
    	$.ajax({
			type : "POST",
			url : basePath + "/bgPortGlutNewsCate/listNewsCate.ido",
			data : {},
			dataType : "json",
			success : function(msg) { 
				if (msg.errCode == '1') {
					createJsTree(msg.data);
				} else {
					toastr.error(msg.errMsg, "提示");
				}
			
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				toastr.error("系统出错！", "提示");
			},
			beforeSend : function(XMLHttpRequest){
				index = layer.load(0, {
					  shade: [0.3,'#ccc'] //0.1透明度的白色背景
					});
			},
			complete : function(XMLHttpRequest, textStatus){
				layer.close(index);
			}
		});
    	
    });
    
    //生成jsTree
    var createJsTree = function(data){
    	$('#jstree').jstree({  
            'plugins' : [ "wholerow", "types" ],  
            'core' : {  
                "themes" : {  
                    "responsive" : false  
                },  
                'data' : data
            },  
            "types" : {  
                "default" : {  
                    "icon" : "fa fa-folder icon-state-warning icon-lg"  
                } 
            }  
        }).on('changed.jstree', function(e, data) {
        	
        	clickTreeNode(data.node);
        });
    }
    
    //点击树节点
    var clickTreeNode = function(node){
    	var data = node.a_attr;
    	
    	$("#mId").val(node.id);
    	$("#mName").val(data.name);
    	$("#mSort").val(data.sort);
    	$("#mCode").val(data.cateId);
    	
    	//获取父节点
    	var pnode = $('#jstree').jstree().get_node(node.parent);
    	$("input[name='pid']").val(pnode.id);
    	$("input[name='pName']").val(pnode.text);
    }
    
    //清空
    var clearForm = function(){
    	$("#mId").val("");
    	$("#mName").val("");
    	$("#mSort").val("");
    	$("#mCode").val("");
    	
    }
    
    //新增
    var add = function(){
    	
    	var id = $("#mId").val();
    	var name = $("#mName").val();
    	clearForm();
    	if(!id || id == 0){
    		$("input[name='pName']").val("新闻分类");
        	$("input[name='pid']").val("0");
        	
    	}else{
    		$("input[name='pName']").val(name);
        	$("input[name='pid']").val(id);
        	
    	}
    	
    	
    }
    
    //删除
    var del = function(){
    	
    	var id = $("#mId").val();
    	
    	if(id == null || id == ''){
    		toastr.warning("请选择需要删除的分类！", "提示");
    		return;
    	}
    	
    	if(id == 0){
    		toastr.warning("不能从根目录删除分类！", "提示");
    		return;
    	}
    	
    	layer.confirm('删除该分类会同时删除其所有下级分类，确定要删除吗?', {icon: 3, title:'提示'}, function(mIndex){
    		
    		$.ajax({
    			type : "POST",
    			url : basePath + "/bgPortGlutNewsCate/delNewsCate.ido",
    			data : {"id" : id},
    			dataType : "json",
    			success : function(msg) { 
    				if (msg.errCode == '1') {
    					toastr.success("操作成功", "提示");
    					window.setTimeout(function(){
    						location.reload();
    					}, 1000);
    				} else {
    					toastr.error(msg.errMsg, "提示");
    				}
    			
    			},
    			error : function(XMLHttpRequest, textStatus, errorThrown) {
    				toastr.error("系统出错！", "提示");
    			},
    			beforeSend : function(XMLHttpRequest){
    				layer.load(0, {
    					  shade: [0.3,'#ccc'] //0.1透明度的白色背景
    					});
    			},
    			complete : function(XMLHttpRequest, textStatus){
    				layer.closeAll();
    			}
    		});
    		  
    	});
    	
    }
    
    //表单提交
    var subForm = function(){
    	debugger;
    	if(document.getElementById("mCode").value == '2c90828f5ad11b39015ad124b8560000'){
    		toastr.warning("队伍概况不能修改!", "提示");
    		return false;
    	}
    	$.ajax({
			type : "POST",
			url : basePath + "/bgPortGlutNewsCate/addNewsCate.ido",
			data : $("#subForm").serialize(),
			dataType : "json",
			success : function(msg) { 
				if (msg.errCode == '1') {
					toastr.success("操作成功", "提示");
					window.setTimeout(function(){
						location.reload();
					}, 1000);
				}else{
					toastr.error(msg.errMsg, "提示");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				toastr.error("系统出错！", "提示");
			},
			beforeSend : function(XMLHttpRequest){
				layer.load(0, {
					  shade: [0.3,'#ccc'] //0.1透明度的白色背景
					});
			},
			complete : function(XMLHttpRequest, textStatus){
				layer.closeAll();
			}
		});
    	
    	return false;
    }
    
    </script>
    


</html>